/*
 * CS_REQ_SERVER_LIST.cpp
 *
 *  Created on: 2013. 11. 5.
 *      Author: the2
 */



#include "../../Command.h"

#include "../../User.h"

#include "../../NLogic/Static.h"

namespace ProjectZ { namespace NCommand {

void Command::CS_REQ_SERVER_LIST(User * pUser, ::Network::Packet * pPacket)
{
	/*
	 *
	//서버리스트 요청 C -> S
	CS_REQ_SERVER_LIST		0x090D
	{

	}

	서버리스트 요청 S -> C
	SC_REQ_SERVER_LIST		0x090E
	{
		u16		u16count;
		{
			u16	u16id;		// 서버 ID
			u8	u8state;	// 서버 상태  0 : 점검중, 1 : 정상
		}
	}
	 */

	PREPARE_PACKET;

	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	::ProjectZ::NLogic::ServerAddrCCU * clsSvrAddr = ::ProjectZ::NLogic::Static::Instance().GETSERVERADDRCCU();
	if(NULL == clsSvrAddr)
	{
		* pAck << static_cast<int16_t>(ACK_NOT_FOUND_SERVER) << static_cast<uint16_t>(0);
		pAck->MakePacket(CMD_SC_REQ_SERVER_LIST);
		pUser->Send(pAck);
		return;
	}

	* pAck << static_cast<int16_t>(ACK_OK) << static_cast<uint16_t>(clsSvrAddr->GetServerAddrTree().size());

	auto & svrAddrTree = clsSvrAddr->GetServerAddrTree();
	std::for_each(svrAddrTree.begin(), svrAddrTree.end(), [&](const std::pair<int, NLogic::ServerAddrCCU::Data > & pair) {
		* pAck << static_cast<uint16_t>(pair.second._serverKey);

//		if(pair.second._timeStamp + 60 < time(NULL))
		if(15 < pair.second._timeStamp)
			* pAck << static_cast<uint8_t>(0);
		else
			* pAck << static_cast<uint8_t>(pair.second._isActive);
	} );
	pAck->MakePacket(CMD_SC_REQ_SERVER_LIST);
	pUser->Send(pAck);
}

} /*NCommand*/ } /*ProjectZ*/

